/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Server.gui;

import Server.domain.ServerDomainController;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author Ripper
 */
public class ServerWindow extends JFrame implements ActionListener
{
    private ServerDomainController sdc;
    
    private JPanel activePanelHolder;
    private ServerPanel activePanel;
    
    private CommandlineScreen commandlineScreen;
    private UserReviewPanel userreviewScreen;
    
    private JButton viewCommandlineScreenButton;
    private JButton viewUserreviewScreenButton;
    //TODO
    
    public ServerWindow()
    {
        super("Project DM - server");
        setLayout(new BorderLayout());
        setSize(800,600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        activePanelHolder = new JPanel(new BorderLayout());
        add(activePanelHolder, BorderLayout.CENTER);
         
        //TODO
        commandlineScreen = new CommandlineScreen(this);
        
        userreviewScreen = new UserReviewPanel(this);
        selectActivePanel(commandlineScreen);
        
        JPanel sideButtonPanel = new JPanel(new GridLayout(2,1));
        {
            viewCommandlineScreenButton = new JButton(" view log ");
            viewCommandlineScreenButton.addActionListener(this);
            sideButtonPanel.add(viewCommandlineScreenButton);
            
            viewUserreviewScreenButton = new JButton(" view users ");
            viewUserreviewScreenButton.addActionListener(this);
            sideButtonPanel.add(viewUserreviewScreenButton);
        }
        add(sideButtonPanel, BorderLayout.WEST);
        
        try
        {
            sdc = new ServerDomainController(this);
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        setLocation((dim.width/2)-getWidth()/2, (dim.height/2)-getHeight()/2);
        
        setVisible(true);
    }
    
    private void selectActivePanel(ServerPanel panel)
    {
        activePanel = panel;
        activePanelHolder.removeAll();
        
        activePanelHolder.add(activePanel, BorderLayout.CENTER);
        validate();
        repaint();
    }
    
    public void logMessage(String msg)
    {
        commandlineScreen.logMessage(msg);
    }
    
    public void submitMessage(String msg)
    {
        logMessage("Admin: "+msg);
        sdc.processMessageFromServerAdmin(msg);
    }
    public ServerDomainController getServerDomainController()
    {
        return sdc;
    }

    @Override
    public void actionPerformed(ActionEvent e) 
    {
        if(e.getSource() == viewCommandlineScreenButton)
        {
            selectActivePanel(commandlineScreen);
        }
        else if(e.getSource() == viewUserreviewScreenButton)
        {
            userreviewScreen.refreshData();
            selectActivePanel(userreviewScreen);
        }
    }
}
